home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-Native.exe / {app} / Samples / Demo4 / src / Sample_Demo4.cpp next >
Encoding:
C/C++ Source or Header  |  2005-08-21  |  17.2 KB  |  404 lines

  1. /************************************************************************
  2.     filename:   Sample_Demo4.cpp
  3.     created:    20/8/2005
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. #include "Sample_Demo4.h"
  25. #include "CEGUI.h"
  26. #include "CEGuiBaseApplication.h"
  27.  
  28. #if defined( __WIN32__ ) || defined( _WIN32 )
  29. #define WIN32_LEAN_AND_MEAN
  30. #include "windows.h"
  31.  
  32. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
  33. #else
  34. int main(int argc, char *argv[])
  35. #endif
  36. {
  37.     // This is a basic start-up for the sample application which is
  38.     // object orientated in nature, so we just need an instance of
  39.     // the CEGuiSample based object and then tell that sample application
  40.     // to run.  All of the samples will use code similar to this in the
  41.     // main/WinMain function.
  42.     Demo4Sample app;
  43.     return app.run();
  44. }
  45.  
  46. /*************************************************************************
  47.     Sample specific initialisation goes here.
  48. *************************************************************************/
  49. bool Demo4Sample::initialiseSample()
  50. {
  51.     using namespace CEGUI;
  52.  
  53.     // we will make extensive use of the WindowManager.
  54.     WindowManager& winMgr = WindowManager::getSingleton();
  55.  
  56.     // load scheme and set up defaults
  57.     SchemeManager::getSingleton().loadScheme("../datafiles/schemes/TaharezLook.scheme");
  58.     System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
  59.     FontManager::getSingleton().createFont("../datafiles/fonts/Commonwealth-10.font");
  60.  
  61.     // load an image to use as a background
  62.     ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "../datafiles/imagesets/GPN-2000-001437.tga");
  63.  
  64.     // here we will use a StaticImage as the root, then we can use it to place a background image
  65.     StaticImage* background = static_cast<StaticImage*>(winMgr.createWindow("TaharezLook/StaticImage", "background_wnd"));
  66.     // set position and size
  67.     background->setPosition(Point(0, 0));
  68.     background->setSize(Size(1, 1));
  69.     // disable frame and standard background
  70.     background->setFrameEnabled(false);
  71.     background->setBackgroundEnabled(false);
  72.     // set the background image
  73.     background->setImage("BackgroundImage", "full_image");
  74.     // install this as the root GUI sheet
  75.     System::getSingleton().setGUISheet(background);
  76.  
  77.     // now we create a DefaultWindow which we will attach all the widgets to.  We could
  78.     // have attached them to the background StaticImage, though we want to be a bit tricky
  79.     // since we do not wish the background to be faded by the slider - so we create this
  80.     // container window so we can affect all the other widgets, but leave the background
  81.     // unchanged.
  82.     Window* sheet = winMgr.createWindow("DefaultWindow", "root_wnd");
  83.     // attach this to the 'real' root
  84.     background->addChildWindow(sheet);
  85.  
  86.     //
  87.     // widget initialisation
  88.     //
  89.     // quit button
  90.     PushButton* btn = static_cast<PushButton*>(winMgr.createWindow("TaharezLook/Button", "QuitButton"));
  91.     sheet->addChildWindow(btn);
  92.     btn->setText("Quit!");
  93.     btn->setPosition(Point(0.035f, 0.0f));
  94.     btn->setSize(Size(0.1f, 0.036f));
  95.     btn->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&Demo4Sample::handleQuit, this));
  96.     btn->setAlwaysOnTop(true);
  97.  
  98.     // Alpha-slider
  99.     Slider* slider = static_cast<Slider*>(winMgr.createWindow("TaharezLook/Slider", "my slider"));
  100.     sheet->addChildWindow(slider);
  101.     slider->setPosition(Point(0.136f, 0.0f));
  102.     slider->setSize(Size(0.02f, 0.075f));
  103.     slider->setAlwaysOnTop(true);
  104.     // here we disable inherited alpha so that we will always be able to see the slider
  105.     slider->setInheritsAlpha(false);
  106.     // set up slider config
  107.     slider->setCurrentValue(1.0f);
  108.     slider->setClickStep(0.1f);
  109.     // subscribe handler that will process slider position changes.
  110.     slider->subscribeEvent(Slider::EventValueChanged, &sliderHandler);
  111.  
  112.     // big scroll bar
  113.     Scrollbar* vscb = static_cast<Scrollbar*>(winMgr.createWindow("TaharezLook/LargeVerticalScrollbar", "Vert Scroll 1"));
  114.     sheet->addChildWindow(vscb);
  115.     vscb->setPosition(Point(0.0f, 0.0f));
  116.     vscb->setMinimumSize(Size(0.01f, 0.01f));
  117.     vscb->setMaximumSize(Size(1.0f, 1.0f));
  118.     vscb->setSize(Size(0.035f, 0.83f));
  119.     vscb->setDocumentSize(100);
  120.     vscb->setPageSize(5);
  121.     vscb->setStepSize(1);
  122.     vscb->setAlwaysOnTop(true);
  123.  
  124.     // mini vert scroll bar
  125.     Scrollbar* mvsb = static_cast<Scrollbar*>(winMgr.createWindow("TaharezLook/VerticalScrollbar", "MiniVertScroll 1"));
  126.     sheet->addChildWindow(mvsb);
  127.     mvsb->setPosition(Point(0.99f, 0.015f));
  128.     mvsb->setMinimumSize(Size(0.01f, 0.01f));
  129.     mvsb->setMaximumSize(Size(1.0f, 1.0f));
  130.     mvsb->setSize(Size(0.01f, 0.5f));
  131.     mvsb->setDocumentSize(360);
  132.     mvsb->setPageSize(45);
  133.     mvsb->setStepSize(1);
  134.     mvsb->setAlwaysOnTop(true);
  135.  
  136.     // mini horz scroll bar
  137.     Scrollbar* mhsb = static_cast<Scrollbar*>(winMgr.createWindow("TaharezLook/HorizontalScrollbar", "MiniHorzScroll 1"));
  138.     sheet->addChildWindow(mhsb);
  139.     mhsb->setPosition(Point(0.485f, 0.0f));
  140.     mhsb->setMinimumSize(Size(0.01f, 0.01f));
  141.     mhsb->setMaximumSize(Size(1.0f, 1.0f));
  142.     mhsb->setSize(Size(0.5f, 0.015f));
  143.     mhsb->setDocumentSize(360);
  144.     mhsb->setPageSize(45);
  145.     mhsb->setStepSize(1);
  146.     mhsb->setAlwaysOnTop(true);
  147.  
  148.     //
  149.     // Build a window with some text and formatting options via radio buttons etc
  150.     //
  151.     FrameWindow* textwnd = static_cast<FrameWindow*>(winMgr.createWindow("TaharezLook/FrameWindow", "TextWindow"));
  152.     sheet->addChildWindow(textwnd);
  153.     textwnd->setPosition(Point(0.2f, 0.2f));
  154.     textwnd->setMaximumSize(Size(0.75f, 0.75f));
  155.     textwnd->setMinimumSize(Size(0.1f, 0.1f));
  156.     textwnd->setSize(Size(0.5f, 0.5f));
  157.     textwnd->setCloseButtonEnabled(false);
  158.     textwnd->setText("Crazy Eddie's GUI - Demo 4");
  159.  
  160.     StaticText* st = static_cast<StaticText*>(winMgr.createWindow("TaharezLook/StaticText", "TextWindow/Static"));
  161.     textwnd->addChildWindow(st);
  162.     st->setPosition(Point(0.1f, 0.2f));
  163.     st->setSize(Size(0.5f, 0.6));
  164.  
  165.     st = static_cast<StaticText*>(winMgr.createWindow("TaharezLook/StaticText", "TextWindow/Group label 1"));
  166.     textwnd->addChildWindow(st);
  167.     st->setPosition(Point(0.65f, 0.23f));
  168.     st->setSize(Size(0.35f, 0.05f));
  169.     st->setText("Horz. Formatting");
  170.     // disable frame and background on static control
  171.     st->setFrameEnabled(false);
  172.     st->setBackgroundEnabled(false);
  173.  
  174.     st = static_cast<StaticText*>(winMgr.createWindow("TaharezLook/StaticText", "TextWindow/Group label 2"));
  175.     textwnd->addChildWindow(st);
  176.     st->setPosition(Point(0.65f, 0.53f));
  177.     st->setSize(Size(0.35f, 0.05f));
  178.     st->setText("Vert. Formatting");
  179.     // disable frame and background on static control
  180.     st->setFrameEnabled(false);
  181.     st->setBackgroundEnabled(false);
  182.  
  183.     st = static_cast<StaticText*>(winMgr.createWindow("TaharezLook/StaticText", "TextWindow/Box label"));
  184.     textwnd->addChildWindow(st);
  185.     st->setPosition(Point(0.12f, 0.13f));
  186.     st->setSize(Size(0.35f, 0.05f));
  187.     st->setText("Formatted Output");
  188.     // disable frame and background on static control
  189.     st->setFrameEnabled(false);
  190.     st->setBackgroundEnabled(false);
  191.  
  192.     // word-wrap checkbox
  193.     Checkbox* cb = static_cast<Checkbox*>(winMgr.createWindow("TaharezLook/Checkbox", "TextWindow/CB1"));
  194.     textwnd->addChildWindow(cb);
  195.     cb->setPosition(Point(0.65f, 0.13f));
  196.     cb->setSize(Size(0.35f, 0.05f));
  197.     cb->setText("Word Wrap");
  198.     // subscribe a handler to listen for when the check-box button select state changes
  199.     cb->subscribeEvent(Checkbox::EventCheckStateChanged, &formatChangedHandler);
  200.  
  201.     // horizontal formatting radio group
  202.     RadioButton* rb = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "TextWindow/RB1"));
  203.     textwnd->addChildWindow(rb);
  204.     rb->setPosition(Point(0.65f, 0.3f));
  205.     rb->setSize(Size(0.35f, 0.05f));
  206.     rb->setGroupID(1);
  207.     rb->setText("Left Aligned");
  208.     // subscribe a handler to listen for when the radio button select state changes
  209.     rb->subscribeEvent(RadioButton::EventSelectStateChanged, &formatChangedHandler);
  210.  
  211.     rb = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "TextWindow/RB2"));
  212.     textwnd->addChildWindow(rb);
  213.     rb->setPosition(Point(0.65f, 0.35f));
  214.     rb->setSize(Size(0.35f, 0.05f));
  215.     rb->setGroupID(1);
  216.     rb->setText("Right Aligned");
  217.     // subscribe a handler to listen for when the radio button select state changes
  218.     rb->subscribeEvent(RadioButton::EventSelectStateChanged, &formatChangedHandler);
  219.  
  220.     rb = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "TextWindow/RB3"));
  221.     textwnd->addChildWindow(rb);
  222.     rb->setPosition(Point(0.65f, 0.4f));
  223.     rb->setSize(Size(0.35f, 0.05f));
  224.     rb->setGroupID(1);
  225.     rb->setText("Centred");
  226.     // subscribe a handler to listen for when the radio button select state changes
  227.     rb->subscribeEvent(RadioButton::EventSelectStateChanged, &formatChangedHandler);
  228.  
  229.     // vertical formatting radio group
  230.     rb = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "TextWindow/RB4"));
  231.     textwnd->addChildWindow(rb);
  232.     rb->setPosition(Point(0.65f, 0.6f));
  233.     rb->setSize(Size(0.35f, 0.05f));
  234.     rb->setGroupID(2);
  235.     rb->setText("Top Aligned");
  236.     // subscribe a handler to listen for when the radio button select state changes
  237.     rb->subscribeEvent(RadioButton::EventSelectStateChanged, &formatChangedHandler);
  238.  
  239.     rb = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "TextWindow/RB5"));
  240.     textwnd->addChildWindow(rb);
  241.     rb->setPosition(Point(0.65f, 0.65f));
  242.     rb->setSize(Size(0.35f, 0.05f));
  243.     rb->setGroupID(2);
  244.     rb->setText("Bottom Aligned");
  245.     // subscribe a handler to listen for when the radio button select state changes
  246.     rb->subscribeEvent(RadioButton::EventSelectStateChanged, &formatChangedHandler);
  247.  
  248.     rb = static_cast<RadioButton*>(winMgr.createWindow("TaharezLook/RadioButton", "TextWindow/RB6"));
  249.     textwnd->addChildWindow(rb);
  250.     rb->setPosition(Point(0.65f, 0.7f));
  251.     rb->setSize(Size(0.35f, 0.05f));
  252.     rb->setGroupID(2);
  253.     rb->setText("Centred");
  254.     // subscribe a handler to listen for when the radio button select state changes
  255.     rb->subscribeEvent(RadioButton::EventSelectStateChanged, &formatChangedHandler);
  256.  
  257.     // Edit box for text entry
  258.     Editbox* eb = static_cast<Editbox*>(winMgr.createWindow("TaharezLook/Editbox", "TextWindow/Editbox1"));
  259.     textwnd->addChildWindow(eb);
  260.     eb->setPosition(Point(0.05f, 0.85f));
  261.     eb->setMaximumSize(Size(1.0f, 0.04f));
  262.     eb->setSize(Size(0.90f, 0.08f));
  263.     // subscribe a handler to listen for when the text changes
  264.     eb->subscribeEvent(Window::EventTextChanged, &textChangedHandler);
  265.  
  266.     //
  267.     // Controls are set up.  Install initial settings
  268.     //
  269.     static_cast<Checkbox*>(winMgr.getWindow("TextWindow/CB1"))->setSelected(true);
  270.     static_cast<RadioButton*>(winMgr.getWindow("TextWindow/RB1"))->setSelected(true);
  271.     static_cast<RadioButton*>(winMgr.getWindow("TextWindow/RB4"))->setSelected(true);
  272.     winMgr.getWindow("TextWindow/Editbox1")->setText("Come on then, edit me!");
  273.  
  274.     // success!
  275.     return true;
  276. }
  277.  
  278. /*************************************************************************
  279.     Cleans up resources allocated in the initialiseSample call.
  280. *************************************************************************/
  281. void Demo4Sample::cleanupSample()
  282. {
  283.     // nothing to do here!
  284. }
  285.  
  286. /*************************************************************************
  287.     Free function to handle slider position changes
  288. *************************************************************************/
  289. bool sliderHandler(const CEGUI::EventArgs& e)
  290. {
  291.     using namespace CEGUI;
  292.  
  293.     // we know it's a slider.
  294.     Slider* s = static_cast<Slider*>(static_cast<const WindowEventArgs&>(e).window);
  295.  
  296.     // get value from slider and set it as the current alpha
  297.     float val = s->getCurrentValue();
  298.     WindowManager::getSingleton().getWindow("root_wnd")->setAlpha(val);
  299.  
  300.     // indicate the event was handled here.
  301.     return true;
  302. }
  303.  
  304. /*************************************************************************
  305.     Free function to handle change of format options
  306. *************************************************************************/
  307. bool formatChangedHandler(const CEGUI::EventArgs& e)
  308. {
  309.     using namespace CEGUI;
  310.  
  311.     // we will use the WindowManager to get access to the widgets
  312.     WindowManager& winMgr = WindowManager::getSingleton();
  313.  
  314.     // get pointers to all the widgets we need to access
  315.     const RadioButton* rb1 = static_cast<const RadioButton*>(winMgr.getWindow("TextWindow/RB1"));
  316.     const RadioButton* rb2 = static_cast<const RadioButton*>(winMgr.getWindow("TextWindow/RB2"));
  317.     const RadioButton* rb3 = static_cast<const RadioButton*>(winMgr.getWindow("TextWindow/RB3"));
  318.     const RadioButton* rb4 = static_cast<const RadioButton*>(winMgr.getWindow("TextWindow/RB4"));
  319.     const RadioButton* rb5 = static_cast<const RadioButton*>(winMgr.getWindow("TextWindow/RB5"));
  320.     const RadioButton* rb6 = static_cast<const RadioButton*>(winMgr.getWindow("TextWindow/RB6"));
  321.     const Checkbox*    cb1 = static_cast<const Checkbox*>(winMgr.getWindow("TextWindow/CB1"));
  322.     // and also the static text for which we will set the formatting options
  323.     StaticText*  st  = static_cast<StaticText*>(winMgr.getWindow("TextWindow/Static"));
  324.  
  325.     // handle vertical formatting settings
  326.     if (rb4->isSelected())
  327.         st->setVerticalFormatting(StaticText::TopAligned);
  328.     else if (rb5->isSelected())
  329.         st->setVerticalFormatting(StaticText::BottomAligned);
  330.     else if (rb6->isSelected())
  331.         st->setVerticalFormatting(StaticText::VertCentred);
  332.  
  333.     // handle horizontal formatting settings
  334.     bool wrap = cb1->isSelected();
  335.  
  336.     if (rb1->isSelected())
  337.         st->setHorizontalFormatting(wrap ? StaticText::WordWrapLeftAligned : StaticText::LeftAligned);
  338.     else if (rb2->isSelected())
  339.         st->setHorizontalFormatting(wrap ? StaticText::WordWrapRightAligned : StaticText::RightAligned);
  340.     else if (rb3->isSelected())
  341.         st->setHorizontalFormatting(wrap ? StaticText::WordWrapCentred : StaticText::HorzCentred);
  342.  
  343.     // event was handled
  344.     return true;
  345. }
  346.  
  347. /*************************************************************************
  348.     Free function handler called when editbox text changes
  349. *************************************************************************/
  350. bool textChangedHandler(const CEGUI::EventArgs& e)
  351. {
  352.     using namespace CEGUI;
  353.  
  354.     //find the static box
  355.     StaticText* st = static_cast<StaticText*>(WindowManager::getSingleton().getWindow("TextWindow/Static"));
  356.  
  357.     // set text from the edit box...
  358.     st->setText(static_cast<const WindowEventArgs&>(e).window->getText());
  359.  
  360.     return true;
  361. }
  362.  
  363. /*************************************************************************
  364.     Handler method that signals the application to quit
  365. *************************************************************************/
  366. bool Demo4Sample::handleQuit(const CEGUI::EventArgs& e)
  367. {
  368.     // signal quit
  369.     d_sampleApp->setQuitting();
  370.  
  371.     // event was handled
  372.     return true;
  373. }
  374.  
  375. /*************************************************************************
  376.     Handler method that ??
  377. *************************************************************************/
  378. bool Demo4Sample::horzMoveHandler(const CEGUI::EventArgs& e)
  379. {
  380.     using namespace CEGUI;
  381.  
  382.     return true;
  383. }
  384.  
  385. /*************************************************************************
  386.     Handler method that ??
  387. *************************************************************************/
  388. bool Demo4Sample::vertMoveHandler(const CEGUI::EventArgs& e)
  389. {
  390.     using namespace CEGUI;
  391.  
  392.     return true;
  393. }
  394.  
  395. /*************************************************************************
  396.     Handler method that ??
  397. *************************************************************************/
  398. bool Demo4Sample::vscrollHandler(const CEGUI::EventArgs& e)
  399. {
  400.     using namespace CEGUI;
  401.  
  402.     return true;
  403. }
  404.